package com.yuma.tingshu.payment.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.yuma.tingshu.common.annotation.YuMaLogin;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.payment.service.TSWxPayService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private TSWxPayService wxPayService;

    /**
     * 创建微信支付二维码
     * @param paymentType
     * @param orderNo
     * @return
     */
    @YuMaLogin
    @PostMapping("/createJsapi/{paymentType}/{orderNo}")
    public Result<Object> createJsapi(@PathVariable("paymentType") String paymentType, @PathVariable("orderNo") String orderNo) {
        Object object = wxPayService.createJsapi(paymentType, orderNo);
        return Result.ok(object);
    }

    /**
     * 微信支付回调
     * @param body
     * @param request
     * @return
     */
    @PostMapping("/payCallback")
    public ResponseEntity payCallback(@RequestBody Map body, HttpServletRequest request) {
        ResponseEntity responseEntity = wxPayService.payCallback(body, request);
        return responseEntity;
    }

    /**
     * 查询支付状态
     * @param orderNo
     * @return
     */
    @GetMapping("queryPayStatus/{orderNo}")
    public Result<Boolean> queryPayStatus(@PathVariable String orderNo) {
        Boolean flag = wxPayService.queryPayStatus(orderNo);
        return Result.ok(flag);
    }

}
